package com.lbx.controller;

import com.lbx.dao.UserDao;
import com.lbx.entity.User;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;


/**
 * @author 吕兵星
 * @date 2020/10/26 21:46
 */
@Controller
public class UserController {
    @Autowired
    private UserDao userDao;

    @GetMapping("/findById")
    @ResponseBody
    public User getUserById(@PathParam("id") Integer id){
        System.out.println(id);
        return userDao.getUserById(id);
    }

    @RequestMapping("/toUpdateUser/{id}")
    public String updateUser(@PathVariable("id")Integer id, @NotNull Model Model){
        System.out.println(id);
        Model.addAttribute("user",userDao.getUserById(id));
        return "updateUser";
    }

    @PostMapping("/updateUser")
    public String updateById(User user){
        System.out.println("updateById");
        userDao.updateUser(user);
        return "redirect:toUserInfo";
    }
    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id") Integer id){

        System.out.println(id);
        userDao.delete(id);
        return "redirect:/toUserInfo";
    }
    @GetMapping("/toAddUser")
    public String toAddUser(){
        return "addUser";
    }

    @PostMapping("/addUser")
    public String addUser(User user){
        userDao.add(user);
        return "redirect:/toUserInfo";
    }
}
